package com.jingnen.guess_index_server.controller;

import com.jingnen.guess_index_server.entity.UserAuth;
import com.jingnen.guess_index_server.service.UserAuthService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user-auth")
public class UserAuthController {

    private final UserAuthService userAuthService;

    public UserAuthController(UserAuthService userAuthService) {
        this.userAuthService = userAuthService;
    }

    // 提交用户登录信息并保存（比如微信登录后后端保存 openid / token 等）
    @PostMapping("/login")
    public ResponseEntity<UserAuth> login(@RequestBody UserAuth userAuth) {
        // 可在 Service 层做更多处理，如关联用户、生成 JWT 等
        UserAuth savedAuth = userAuthService.saveUserAuth(userAuth);
        return ResponseEntity.ok(savedAuth);
    }

    // 根据用户ID和登录类型查询认证信息（比如判断某个用户是否已绑定微信）
    @GetMapping("/user/{userId}/type/{loginType}")
    public ResponseEntity<UserAuth> getUserAuth(@PathVariable Long userId, @PathVariable String loginType) {
        var authOpt = userAuthService.getUserAuthByUserIdAndLoginType(userId, loginType);
        return authOpt
                .map(ResponseEntity::ok)
                .orElseGet(() -> ResponseEntity.notFound().build());
    }
}
